#pragma once

#ifdef __cplusplus
extern "C" {
#endif
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavfilter/buffersink.h>
#include <libavfilter/buffersrc.h>
#include <libavutil/opt.h>
#include <libavutil/log.h>
#ifdef __cplusplus
}
#endif

#include <string>
#include "statistic_latency.h"

class Output_Stream
{
    public:
        Output_Stream(const char* url);
        bool add_stream(AVCodecParameters* AVCodecParameters, AVRational& time_base);
        int write_audio_frame(AVPacket* packet);
        int write_video_frame(AVPacket* packet);
        ~Output_Stream();

        void statistic_info(int force);

    private:
        std::string m_url;
        AVFormatContext *m_format_context = NULL;
        bool m_open_success = false;
        bool m_write_header = false;
        bool m_write_error = false;
        AVStream* m_video_stream =  nullptr;
        AVStream* m_audio_stream = nullptr;
        bool m_write_idr = false;
        int64_t m_audio_last_dts = 0;
        int64_t m_audio_last_pts = 0;

        int64_t m_video_last_dts = 0;
        int64_t m_video_last_pts = 0;

        //for statistics purposes only
        latency_context_t* p_video_latency;
        ///latency_context_t* p_audio_latency;
        timeval m_start_time;
        timeval m_last_time;
        int64_t m_video_frame_out_cnt = 0;
        int64_t m_audio_frame_out_cnt =0;
};